/* $RCSFile: Company.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.ServerControls/Company.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:30  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Diagnostics;
using System.Globalization;

namespace ADNCRM.ServerControls
{
    public class Company : ConfigurationSection
    {
        public static void CreateSection(string CompanyName,string productName,string pin)
        {
            if ( LicenseTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]Company CreateSection CompanyName: {0}, productName: {1}, pin: {2}", CompanyName,productName,pin),LicenseTraceSwitch.Sw.Info);
            try
            {

                Company customSection;

                // Get the current configuration file.
                Configuration config                            =   WebConfigurationManager.OpenWebConfiguration("~");

                // Create the section entry  
                // in <configSections> and the 
                // related target section in <configuration>.
                // License is the Group->could register several companies, not available yet
                ConfigurationSectionGroup group  =    config.GetSectionGroup("License");
                if ( group == null)
                {
                    if ( LicenseTraceSwitch.Sw.TraceVerbose )
                        Trace.WriteLine( "[V]Company CreateSectionLicenseKey creating group.",LicenseTraceSwitch.Sw.Info);
                    customSection                               =   new Company();
                    customSection.Name                          =   CompanyName;
                    LicenseKey lic                              =   new LicenseKey(productName,pin);
                    customSection.Licenses.Add(lic);
                    group                                       =   new ConfigurationSectionGroup();
                    group.Sections.Add("Company", customSection);
                    config.SectionGroups.Add("License",group);
                    if ( LicenseTraceSwitch.Sw.TraceVerbose )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]Company CreateSection name: {0} created", customSection.SectionInformation.Name),LicenseTraceSwitch.Sw.Info);
                }
                else
                {
                    if ( LicenseTraceSwitch.Sw.TraceVerbose )
                        Trace.WriteLine( "[V]Company CreateSectionLicenseKey group found.",LicenseTraceSwitch.Sw.Info);
                    customSection                               =   (Company)group.Sections.Get("Company");
                    if ( LicenseTraceSwitch.Sw.TraceVerbose )
                        Trace.WriteLine( "[V]Company CreateSectionLicenseKey section Company found.",LicenseTraceSwitch.Sw.Info);
                    if ( customSection.Licenses[productName] == null )
                    {
                        LicenseKey lic                          =   new LicenseKey(productName,pin);
                        customSection.Licenses.Add(lic);
                        if ( LicenseTraceSwitch.Sw.TraceVerbose )
                            Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]Company CreateSectionLicenseKey added product: {0}, Pin: {1}",productName,pin),LicenseTraceSwitch.Sw.Info);
                    }
                    else
                    {
                        LicenseKey lic                          =   customSection.Licenses[productName];
                        if ( LicenseTraceSwitch.Sw.TraceVerbose )
                            Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]Company CreateSectionLicenseKey found product: {0}, Pin: {1}",productName,lic.LicensePIN),LicenseTraceSwitch.Sw.Info);
                        lic.LicensePIN                          =   pin;
                        if ( LicenseTraceSwitch.Sw.TraceVerbose )
                            Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]Company CreateSectionLicenseKey modified product: {0}, Pin: {1}",productName,pin),LicenseTraceSwitch.Sw.Info);
                    }
                }
                customSection.SectionInformation.ForceSave      =   true;
                config.Save(ConfigurationSaveMode.Full);

                if ( LicenseTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]Company CreateSection section saved Name: {0}.", customSection.SectionInformation.Name),LicenseTraceSwitch.Sw.Info);
            }
            catch (Exception ex)
            {
                if ( LicenseTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]Company CreateSection ex: {0}", ex),LicenseTraceSwitch.Sw.Info);
                throw;
            }
            if ( LicenseTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]Company CreateSection END",LicenseTraceSwitch.Sw.Info);
        }
        
        public static Company GetConfig()
        {
            return ConfigurationManager.GetSection("License/Company") as Company;
        }

        [ConfigurationProperty("Licenses")]
        public LicenseKeysCollection Licenses
        {
            get
            {
                return this["Licenses"] as LicenseKeysCollection;
            }
        }

        [ConfigurationProperty("name", 
            DefaultValue = "AddonNice",
            IsRequired = true, 
            IsKey = false)]
        [StringValidator(InvalidCharacters = 
            " ~!@#$%^&*()[]{}/;'\"|\\",
            MinLength = 1, MaxLength = 60)]        
        public string Name
        {
            get
            {
                return this["name"] as string;
            }
            set
            {
                this["name"] = value;
            }
        }
   }
}